<?php

namespace app\commission;

use app\models\order\Order;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\user\User;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class Sale
{
    private $order;

    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    public function run()
    {
        if ($this->order->return_sale != 2) {
            $this->exe();
        }
    }

    private function exe()
    {
        // $list = Order::find()
        //     // 昨天
        //     ->where(['<=', 'TO_DAYS(NOW( )) - TO_DAYS(create_time)', 1])
        //     // 今天
        //     // ->where(['DATE_FORMAT(create_time, "%Y-%m-%d")' => date('Y-m-d')])
        //     ->andWhere(['return_sale' => 1])
        //     ->all();
        //
        // $total = 0;
        // foreach ($list as $order) {
        //     $total += $order->total_pay - $order->total_freight;
        // }

        $total = $this->order->total_pay - $this->order->total_freight;

        $svip1 = Chain::getUsersByLevel(UserLevel::$keys['svip1'], true);
        $svip2 = Chain::getUsersByLevel(UserLevel::$keys['svip2'], true);

        $v1 = Chain::getUsersByLevel(UserLevel::$keys['v1'], true);
        $v2 = Chain::getUsersByLevel(UserLevel::$keys['v2'], true);
        $v3 = Chain::getUsersByLevel(UserLevel::$keys['v3'], true);
        $v5 = Chain::getUsersByLevel(UserLevel::$keys['v5'], true);

        $w1 = Chain::getUsersByLevel(UserLevel::$keys['w1'], true);
        $w2 = Chain::getUsersByLevel(UserLevel::$keys['w2'], true);
        $w3 = Chain::getUsersByLevel(UserLevel::$keys['w3'], true);

        // var_dump($w1);exit;

        $svip1_ratio = 0.01;
        $svip2_ratio = 0.015;

        $v1_ratio = 0.02;
        $v2_ratio = 0.03;
        $v3_ratio = 0.04;
        $v5_ratio = 0.06;

        $w1_ratio = 0.02;
        $w2_ratio = 0.03;
        $w3_ratio = 0.04;

        if ($svip1) {
            $balance = $svip1_ratio * $total / count($svip1);
            $this->item($svip1, $balance);
        }
        if ($svip2) {
            $balance = $svip2_ratio * $total / count($svip2);
            $this->item($svip2, $balance);
        }
        if ($v1) {
            $balance = $v1_ratio * $total / count($v1);
            $this->item($v1, $balance);
        }
        if ($v2) {
            $balance = $v2_ratio * $total / count($v2);
            $this->item($v2, $balance);
        }
        if ($v3) {
            $balance = $v3_ratio * $total / count($v3);
            $this->item($v3, $balance);
        }
        if ($v5) {
            $balance = $v5_ratio * $total / count($v5);
            $this->item($v5, $balance);
        }
        if ($w1) {
            $balance = $w1_ratio * $total / count($w1);
            $this->item($w1, $balance);
        }
        if ($w2) {
            $balance = $w2_ratio * $total / count($w2);
            $this->item($w2, $balance);
        }
        if ($w3) {
            $balance = $w3_ratio * $total / count($w3);
            $this->item($w3, $balance);
        }

        // return status
        // foreach ($list as $order) {
        //     $order->return_sale = 2;
        //     $order->save();
        // }
        $this->order->return_sale = 2;
        $this->order->save();
    }

    private function item($list, $balance)
    {
        foreach ($list as $v) {

            $name = $v['level'] >= UserLevel::$keys['w1']
                ? UserCommission::$AWARD_NAME['SALE_W']
                : UserCommission::$AWARD_NAME['SALE_V'];
            $type = $v['level'] >= UserLevel::$keys['w1']
                ? UserCommission::$AWARD_Type['SALE_W']
                : UserCommission::$AWARD_Type['SALE_V'];

            $model = new UserCommission();
            $model->user_id = $v['id'];
            $model->fans_id = $this->order->user_id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = 1;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            Commission::getInstance()->commissionSaveAccount($model);

            // reset sale
            $this->resetSale($v);
        }
    }

    private function resetSale(User $user)
    {
        $commission = UserCommission::find()
            ->where(['user_id' => $user->id])
            ->sum('balance');

        $amount = 0;
        $order = Order::find()
            ->where(['user_id' => $user->id])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->with(['item'])
            ->asArray()
            ->all();

        // brand area order
        foreach ($order as $v) {
            $product_id = $v['item'][0]['product_id'];
            if (ProductAreaList::checkInArea($product_id, Product::$areaMap['BRAND'])) {
                $amount += $v['total_pay'] - $v['total_freight'];
            }
        }

        $user->order_amount = $amount;
        $user->sale_commission = $commission;
        $user->save();

        $num = 5;
        if ($commission - $amount * $num > 0) {
            $user->reset_sale = 1;
            $user->save();
        }
    }
}